﻿using System;
using System.Net;
using System.Text;
using AgentListener;

namespace TestApp
{
    class Program
    {
        private const string DumpString = "{" +
                                          "AgentId : \"5090222c152d7d06e8e22e64\",SysHostName : \"localhost.localdomain\"," +
                                          "SysDiskSpc : [{'MountPoint' : \"/\",'Total' : \"2000\",'Used' : \"1000\"},{'MountPoint' : \"/var\",'Total' : \"3500\",'Used' : \"3000\"}]," +
                                          "SysUptime : \"some shit here\"," +
                                          //"Timestamp : \"2012-11-25T23:30:02.9145239+04:00\"" +
                                          "Timestamp : \"2011-09-19T04:00:10.112Z\"" +
                                          "}";

        static void Main(string[] args)
        {
            var list = ContainerManager.GetListener();
            list.Start();

            Console.WriteLine("Type 'quit' to quit.");
            string cmd;
            while ((cmd = Console.ReadLine()) != "quit")
            {
                WebRequest request = null;
                switch (cmd)
                {
                    case "sync":
                        request = WebRequest.Create("http://localhost:80/sync?sysID=1&agentID=1&action=GET_METRICS");
                        break;
                    case "data":
                        request = WebRequest.Create("http://localhost:80/data?agentId=5090222c152d7d06e8e22e64");
                        break;
                }

                if (request == null)
                    continue;

                Console.WriteLine("[{0:HH:mm:ss}] Sending request...", DateTime.Now);

                request.Method = "POST";
                request.Timeout = 3000;
                var stream = request.GetRequestStream();
                var buffer = Encoding.Default.GetBytes(DumpString);
                stream.Write(buffer, 0, buffer.Length);

                request.BeginGetResponse(Callback, request);
            }

            list.Stop();
        }

        private static void Callback(IAsyncResult ar)
        {
            var request = (WebRequest)ar.AsyncState;
            try { var result = request.EndGetResponse(ar); }
            catch (Exception e) { Console.WriteLine(e); }
        }
    }
}